<template>
  <div class="home_game_hot_box">
    <div class="card_list_box">
      <ul class="card_title">
        <li class="title_left">
          <ImgT src="/imgs/home/saishi.webp" alt="" />
          <span v-if="tabInfo == 3 || tabInfo == 4">{{ t('home_page_hot_event') }}</span>
          <span v-else>
            {{ t("home_page_hot_event_name", { name: unserialize(tabInfo.name, false) }) }}
          </span>
        </li>
      </ul>
    </div>
    <GameList 
      :games="hotBoxGames" 
      :type="tabInfo"
      :gameWidth="(tabInfo == 3 || tabInfo == 4)  ? 168 : 82"
      :gameHeight="(tabInfo == 3 || tabInfo == 4) ? 148 : 108"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, watch, defineProps, onBeforeMount, onUnmounted } from 'vue';
import GameList from "./gameList.vue";
import { unserialize } from '@/utils/useStoreMethods';

import { NetPacket } from '@/netBase/NetPacket';
import H5Websocket from "@/net/Net1";
import { NetMsgType } from '@/netBase/NetMsgType';
import { MessageEvent2 } from '@/net/MessageEvent2';
import { storeToRefs } from 'pinia';
import pinia from '@/store/index';
import { Page } from '@/store/page';

const {  hotBoxGames  } = storeToRefs(Page(pinia));

import { useI18n } from 'vue-i18n';
const { t } = useI18n();

const props: any = defineProps({
  tabInfo: {
    type: Object,
    default: () => ({}), // 设置默认值
  },
});

console.log(unserialize(props.tabInfo.name, false), props.tabInfo.id)

const tabInfo = ref({ ...props.tabInfo });

const state = reactive({
  // 游戏平台id  -1为查看全部的游戏
  agentId: -1,

  // 自定义标签 0为全部 1为热门 -2为收藏，-3为最近
  lableId: 1,

  //也表示场馆id -1 为所有场馆
  kindIdHotBox: props.tabInfo.id,

  // 当前点击的游戏
  currentGame: null as any,
})

const params: any = reactive({ // 参数
  pageSize: 20,
  page: 1,
})
const result: any = ref({ // 结果
  total_page: 0,
  // 游戏集合
  list: []
})

const resetData = () => {
  params.page = 1
  result.list = []
}

// 返回游戏列表数据
const handleGames = async (res: any) => {
  // [...JSON.parse(JSON.stringify(games.value)), ...JSON.parse(JSON.stringify(res.info))]
  await Page(pinia).updateHotBoxGames(res.info);
  MessageEvent2.removeMsgEvent(NetMsgType.msgType.msg_notify_get_games_in_platform, null);
}

const queryHotBoxData = () => { // 查询
  const query = NetPacket.req_get_games_in_platform()
  query.agentId = Number(state.agentId)
  query.kindId = state.kindIdHotBox
  query.lableId = state.lableId
  query.page = params.page
  query.pageSize = params.pageSize
  H5Websocket.sendMessageHandler(query);
  MessageEvent2.addMsgEvent(
    NetMsgType.msgType.msg_notify_get_games_in_platform,
    handleGames
  );
}

const handleEventsList = async (res: any) => {
  await Page(pinia).updateHotBoxGames(res.tournm_list);
};

const queryHotEventsData = () => {
  const eventsList = NetPacket.req_tournament_events_list();
  eventsList.game_type = 0;
  eventsList.page = 20;
  H5Websocket.sendMessageHandler(eventsList);
  MessageEvent2.addMsgEvent(
    NetMsgType.msgType.msg_notify_tournament_events_list,
    handleEventsList
  );
}

watch(
  () => props.tabInfo.id,
  (newId) => {
    tabInfo.value = newId
    state.kindIdHotBox = newId;
    setTimeout(() => {
      resetData(); // 清空列表数据
      if (newId === 3 || newId === 4) {
        queryHotEventsData();
      } else {
        queryHotBoxData();
      }
    }, 300)
  },
  { immediate: true }
);

onBeforeMount(() => {
  resetData();
  if (tabInfo.value.id === 3 || tabInfo.value.id === 4) {
    queryHotEventsData();
  } else if(tabInfo.value === 1) {
    queryHotBoxData();
  }
});

onUnmounted(() => {
  MessageEvent2.removeMsgEvent(NetMsgType.msgType.msg_notify_tournament_events_list, null);
})
</script>

<style lang="scss" scoped>
.home_game_hot_box {

  .card_list_box {
    padding-left: 14px;
    margin: 0 auto;

    .card_title {
      display: flex;
      padding: 0 14px;
      justify-content: space-between;
      align-items: center;
      height: 24px;
      line-height: 24px;
      margin: 12px 0;

      .title_left {
        font-family: PingFang SC;
        font-size: 14px;
        font-weight: 600;
        line-height: 16px;
        text-align: left;
        color: #FCFFFA;
        display: flex;
        justify-content: space-between;
        align-items: center;

        img {
          width: 18px;
          height: 18px;
          margin-right: 5px;
        }
      }

      .title_right {
        font-family: PingFang SC;
        font-size: 12px;
        font-weight: 500;
        line-height: 12px;
        text-align: left;
        color: #C6C4F5;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .more {
          background: #282747;
          margin-left: 8px;
          padding: 8px;
          border-radius: 5px;
          box-shadow: 0.5px 0.5px 1px 0px #4B4A6E inset;
        }
      }
    }
  }
  .hot_game_list {
    width: 347px;
    margin: 0 auto;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1px;

    .all_game {
      width: 82px;
      height: 108px;
      margin-right: 6px;
      margin-bottom: 6px;
      background: #1A1944;
      border-radius: 8px;
      position: relative;

      .bg_img {
        width: 82px;
        height: 82px;
      }

      .game_name {
        width: 100%;
        margin-top: 0px;
        text-align: center;
        overflow: hidden; //超出的文本隐藏
        text-overflow: ellipsis; //溢出用省略号显示
        white-space: nowrap; //溢出不换行
      }
      .collect_img {
        position: absolute;
        top: 4px;
        right: 4px;
      }
      .masking_layer {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        backdrop-filter: blur(1px);
        background: rgba(0, 0, 0, 0.6);
        font-family: PingFang SC;
        font-size: 12px;
        font-weight: 500;
        line-height: 18px;
        color: #ffffff;

        .maskin_img {
          width: 32px;
          height: 32px;
          margin-bottom: 8px;
        }
      }
    }
  }
}

</style>